﻿
// <copyright file="SafeCollectionDecorator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The safe collection decorator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Concurrent
{
    using System.Collections;
    using System.Collections.Generic;

    using SubhadraSolutions.Sharp.Utils.Threading;
    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// The safe collection decorator.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class SafeCollectionDecorator<T> : AbstractReaderWriterAtomicOperationSupported, ICollection<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly ICollection<T> adaptedObject;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="SafeCollectionDecorator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        public SafeCollectionDecorator(ICollection<T> adaptedObject)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            this.adaptedObject = adaptedObject;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the count.
        /// </summary>
        public int Count
        {
            get
            {
                try
                {
                    this.LockSlim.EnterReadLock();
                    return this.adaptedObject.Count;
                }
                finally
                {
                    this.LockSlim.ExitReadLock();
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether is read only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                try
                {
                    this.LockSlim.EnterReadLock();
                    return this.adaptedObject.IsReadOnly;
                }
                finally
                {
                    this.LockSlim.ExitReadLock();
                }
            }
        }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        public void Add(T item)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                this.adaptedObject.Add(item);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        /// <summary>
        /// The clear.
        /// </summary>
        public void Clear()
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                this.adaptedObject.Clear();
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        /// <summary>
        /// The contains.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Contains(T item)
        {
            try
            {
                this.LockSlim.EnterReadLock();
                return this.adaptedObject.Contains(item);
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The copy to.
        /// </summary>
        /// <param name="array">
        /// The array.
        /// </param>
        /// <param name="arrayIndex">
        /// The array index.
        /// </param>
        public void CopyTo(T[] array, int arrayIndex)
        {
            try
            {
                this.LockSlim.EnterReadLock();
                this.adaptedObject.CopyTo(array, arrayIndex);
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            return this.GetEnumeratorCore();
        }

        /// <summary>
        /// The remove.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Remove(T item)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                return this.adaptedObject.Remove(item);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumeratorCore();
        }

        #endregion Explicit Interface Methods

        #region Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        private IEnumerator<T> GetEnumeratorCore()
        {
            IEnumerator<T> enumerator = this.adaptedObject.GetEnumerator();
            enumerator = new SafeEnumeratorDecorator<T>(enumerator, this.LockSlim);
            return enumerator;
        }

        #endregion Methods
    }
}